package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.exce.SubjectData;
import com.atguigu.eduservice.entity.vo.subject.OneSubject;
import com.atguigu.eduservice.entity.vo.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListenerxtends;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-06-21
 */
@Slf4j
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try{
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListenerxtends(subjectService)).sheet().doRead();

        } catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        //查询一级分类
        QueryWrapper<EduSubject> oneWrapper = new QueryWrapper<>();
        oneWrapper.eq("parent_id", "0");
        List<EduSubject> oneEduSubjects = baseMapper.selectList(oneWrapper);

        //查询二级分类
        QueryWrapper<EduSubject> twoWrapper = new QueryWrapper<>();
        twoWrapper.ne("parent_id", "0");
        List<EduSubject> towEduSubjects = baseMapper.selectList(twoWrapper);

        List<OneSubject> finalSubjectList = new ArrayList<>();
//        finalSubjectList = oneEduSubjects.stream()
//                .map(eduSubject -> {
//                    OneSubject oneSubject = new OneSubject();
//                    BeanUtils.copyProperties(eduSubject, oneSubject);
//                    oneSubject.setChildren(
//                            towEduSubjects.stream()
//                                    .filter(eduSubject1 -> eduSubject.getId().equals(eduSubject1.getParentId()))
//                                    .map(eduSubject1 -> {
//                                        TwoSubject twoSubject = new TwoSubject();
//                                        BeanUtils.copyProperties(eduSubject1, twoSubject);
//                                        return twoSubject;
//                                    })
//                                    .collect(Collectors.toList())
//                    );
//                    return oneSubject;
//                })
//                .collect(Collectors.toList());
        //错误代码
        for (int i = 0; i < oneEduSubjects.size(); i++) {
            EduSubject eduSubject = oneEduSubjects.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject,oneSubject);
                for (int j = 0; j < towEduSubjects.size(); j++) {
                    EduSubject eduSubject1 = towEduSubjects.get(j);
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduSubject1,twoSubject);
                    if(oneSubject.getId().equals(eduSubject1.getParentId())){
                        oneSubject.getChildren().add(twoSubject);
                    }
                }
            finalSubjectList.add(oneSubject);
        }
        return finalSubjectList;
    }
}
